using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using GarageGames.Torque.T2D;
using GarageGames.Torque.Materials;
using System.Collections;

namespace Xyne.Utilities
{
    public class AnimationManager
    {
        #region Variables
        private Hashtable _animationDataHash;
        #endregion

        #region Instance Definition
        private static AnimationManager _instance;

        public static AnimationManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AnimationManager();
                }

                return _instance;
            }
        }
        #endregion

        #region Constructor
        public AnimationManager()
        {
            
        }
        #endregion

        #region Methods
        #region INIT
        public void Init()
        {
            _animationDataHash = new Hashtable();
            StreamReader animationInput = new StreamReader(@"data\images\AnimationData.txt");

            string inputString = animationInput.ReadLine();
            try
            {
                while (!string.IsNullOrEmpty(inputString))
                {
                    string[] animationDataStringSplit = inputString.Split('|');
                    if (animationDataStringSplit.Length > 1)
                    {
                        string animationPath = @"data\images\" + animationDataStringSplit[0];
                        string animationName = animationPath.Substring(animationPath.LastIndexOf('\\')+1, animationPath.LastIndexOf('.')-animationPath.LastIndexOf('\\')-1);
                        int cellCountX = int.Parse(animationDataStringSplit[1]);
                        
                        string animationFramesString = GetAnimationFramesString(cellCountX);

                        
                        SimpleMaterial material = new SimpleMaterial();
                        material.TextureFilename = animationPath;
                        T2DAnimationData data = new T2DAnimationData();
                        CellCountDivider divider = new CellCountDivider();
                        divider.CellCountX = cellCountX;
                        divider.CellCountY = 1;
                        material.TextureDivider = divider;
                        data.Material = material;
                        data.AnimationDuration = 1.0f;
                        data.AnimationFrames = animationFramesString;
                        data.Init();
                        _animationDataHash.Add(animationName, data);
                    }
                    inputString = animationInput.ReadLine();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                animationInput.Close();
            }


        }

        private string GetAnimationFramesString(int cellCount)
        {

            string animationFramesString = string.Empty;

            for (int i = 0; i < cellCount; ++i)
            {
                animationFramesString += i.ToString() + " ";
            }
            animationFramesString = animationFramesString.TrimEnd(' ');

            return animationFramesString;

        }
        #endregion

        #region Accessors
        public T2DAnimationData GetAnimationData(string animationName)
        {
            return (T2DAnimationData)_animationDataHash[animationName];
        }
        #endregion
        #endregion


    }
}